<?php

namespace app\partner\controller;


use think\Controller;
use think\Db;
use think\Session;

class Index extends Authorized
{

    public function index()
    {

        return $this->fetch();
    }

    public function console()
    {
        $project=Db::table('project')->order('sort desc,id desc')->select();
        $this->assign('project', $project);
        return $this->fetch();
    }
    public function task_add_Form()
    {
        $get_data = $this->request->get();
        if($get_data){
            $this->assign('company_id', $get_data['company_id']);
            $this->assign('project_id', $get_data['project_id']);
        }else{
            return '';
        }
        return $this->fetch();
    }

    public function messages()
    {
        return $this->fetch();
    }

//    api
    //    增加签单 api
    public function add_task()
    {
        $post_data = $this->request->post();
        if($post_data){

            $post_data['created_time']=date('y-m-d h:i:s',time());
            Db::table('task')->startTrans();
            $id =Db::table('task')->insertGetId($post_data);
            if (false === $id) {
                Db::table('task')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'添加失败','data'=>'']);
            } else {
                Db::table('task')->commit();
                return json(['code' =>0,'count' =>0,'msg' =>'添加成功','data'=>'']);
            }

        }


        return $this->fetch();
    }
    //项目详情 api
    public function show_project()
    {
        $id = input('id');
        if($id){
            $rows=Db::table('project')->where('id',$id)->find();
            return json(['code' =>0,'count' =>'','msg'=>'','data'=>$rows]);
        }
    }





    public function change_pwd()
    {
        $data = $this->request->post();
        if($data){
            $res = Db::table('company')
                ->where([
                    'id' => Session::get('user.id'),
                    'password'=>md5($data['oldPassword'])
                ])->find();
            if ($res) {
                $result = Db::table('company')
                    ->where([
                        'id' => Session::get('user.id')
                    ])
                    ->setField('password', md5($data['password']));
                if ($result) {
                    return json(['msg' =>'修改成功', 'code' =>0]);
                }else{
                    return json(['msg' =>'修改失败', 'code' =>1]);
                }

            }else{
                return json(['msg' =>'旧密码错误', 'code' =>1]);
            }
        }else{
            return $this->fetch();
        }

    }

    public function info()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('company')->where('id', $post_data['id'])->setField($post_data);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }

        $article=Db::table('company')->where('id' , input('id'))->find();
        $this->assign('article', $article);

        return $this->fetch();
    }




}